/*   MAIN     Main function for Munimatch.
     Copyright (C) 1997  Filip Hroch, Masaryk University, Brno, CZ

     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

    E-mail: hroch@physics.muni.cz
    Web:    http://www.sci.muni.cz/~hroch/index.html
    Postal address: F.Hroch
                    Department of Theoretical Physics and Astrophysics
                    Masaryk University
                    Kotlarska 2
                    611 37 Brno
                    Czech Republic
*/
/*
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C                                                                      C
C !!!!!!!!!!!!!!!!     This is UNIX version  !!!!!!!!!!!!!!!!!!!!!!!!! C
C                                                                      C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

   Find cross-references between the CCD frames or between the CCD frame
   and the catalogue.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define LEN 80

main(argc,argv)
int argc;
char *argv[];
{
FILE *fopen(),*fp;
int i;
char namsci[LEN]="",ref[LEN]="",line[LEN];
char pary[10] = "          ", trapar[10]="          ";
char *j;
int stdinput = 0;

printf("MUNIMATCH Version 1.12, Copyright (C) 1997 F.Hroch, Masaryk University, Brno, CZ\n\r");
printf("Munimatch comes with ABSOLUTELY NO WARRANTY; This is free software, and you are\n\r");
printf("welcome to redistribute under certain conditions; see 'gnulic.doc' for details.\n\r\n\r");

for( i = 0; i < argc; i++) {
  if( (j = strstr(argv[i],"@")) != NULL ) {         /* dir file */
     sprintf(namsci,"%s",j + 1);
     if( namsci[0] == '\0' ) stdinput = 1; }
  if( (j = strstr(argv[i],"ref=")) != NULL )   /* reference file */
     sprintf(ref,"%s",j + 4); 
  if( (j = strstr(argv[i],"cat=")) != NULL ) { /* reference file */
     sprintf(ref,"%s",j + 4); 
     pary[0] = 'C'; }
  if( (j = strstr(argv[i],"-k")) != NULL )    /* change output format */
     pary[1] = 'K'; 
  if( (j = strstr(argv[i],"-t")) != NULL )    /* print transformation */
     trapar[1] = 'T'; }

if( strlen(ref) <= 0 && argc > 1 ) {
  printf("Missed name of the reference file.");
  exit(0); }

if( namsci[0] != '\0' ) {                 /* image names from file */
  if( (fp = fopen(namsci,"r")) == NULL ) {
    printf("Can't open file %s\n\r",namsci);
    exit(0); }
  while( fscanf(fp,"%s",line) != EOF ) {
    printf("\n\r*************************************************************************\n\r\n\r");
    tri_(ref,line,pary,trapar); }
  fclose(fp); }

for( i = 1; i < argc; i++)       /* image names from command line */
  if( strstr(argv[i],"=") == NULL && strstr(argv[i],"@") == NULL 
   && strstr(argv[i],"-") == NULL) {
    printf("\n\r*************************************************************************\n\r\n\r");
    tri_(ref,argv[i],pary,trapar); }

if( stdinput )               /* images names from standart input */
  while( fgets(line,LEN,stdin) != NULL ) {
    printf("\n\r*************************************************************************\n\r\n\r");
    tri_(ref,line,pary,trapar); }

if( argc == 1 ) {
  printf("MUNIMATCH   Find cross-references between the catalogues.\n\r\n\r");
  printf("Usage:munimat [@dirfile] ref=refcat [cat1] ...\n\r\n\r");
  printf("@dirfile= file with name(s) of stars catalogue(s)\n\r");
  printf("          (for only '@', read catalogue(s) name(s) from standart input)\n\r");
  printf("refcat= reference catalogue\n\r");
  printf("cat1...= name(s) of catalogue(s)\n\r\n\r");
  printf("'Catalogue' is file with numbers and coordinates of a stars (in free format)\n\r");
  printf("or any output file from DAOPHOT (MUNIPHOT).\n\r");
  printf("Example of a catalogue:\n\r");
  printf(" 1 10.1 20.2\n\r");
  printf(" 2 30.3 40.4\n\r");
  printf(" 3 50.5 60.6\n\r\n\r");
  printf("You can sort catalogue according to magnitudes of the stars before MUNIMAT run.\n\r");
  printf("MUNIMAT will work probable most successfully.\n\r");
}

}
